package com.spark.lola.controller;

import com.spark.lola.entity.SupplierToken;
import com.spark.lola.entity.sup.AccountInfo;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.service.pur.impl.OrderInfoServiceImpl;
import com.spark.lola.service.sup.IAccountInfoService;
import com.spark.lola.service.sup.ISupplierInfoService;
import com.spark.lola.support.api.constant.ApiConstants;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.core.param.UpdatePaswordRequest;
import com.spark.lola.support.wms.cache.CollectionCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 账户相关
 * @Author: DengYeJun
 * @Date: 2020/5/22 14:26
 */
@Api(value = "/api/acc/", tags = "账户相关")
@RequestMapping("/api/acc/")
@RestController
public class AccountController {
    private static final Logger logger = LoggerFactory.getLogger(OrderInfoServiceImpl.class);

    @Autowired
    private IAccountInfoService accountInfoService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private ISupplierInfoService supplierInfoService;
    /**
     * 获取用户信息
     * @return
     */
    @ApiOperation(value = "获取用户信息")
    @RequestMapping(value = "/info",method = RequestMethod.POST)
    public ResponseBean info(@SessionAttribute SupplierToken tokenUser) {
        AccountInfo model = accountInfoService.getById(tokenUser.getMemberId());
        return ResponseBean.success(model);
    }

    /**
     * 获取用户经营主体信息
     * @return
     */
    @ApiOperation(value = "获取用户经营主体信息")
    @RequestMapping(value = "/main",method = RequestMethod.POST)
    public ResponseBean main(@SessionAttribute SupplierToken tokenUser) {
        SupplierInfo model = supplierInfoService.getById(tokenUser.getSupplierId());
        if (model.getSalesmanId() != null) {
            if(CollectionCache.employees.containsKey(model.getSalesmanId())) {
                model.setSalesman(CollectionCache.employees.get(model.getSalesmanId()));
            }
        }
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "更新商户地址信息")
    @RequestMapping(value = "/main/update",method = RequestMethod.POST)
    public ResponseBean updateMain(@RequestBody SupplierInfo form, @SessionAttribute SupplierToken tokenUser){
        SupplierInfo model = supplierInfoService.getById(tokenUser.getSupplierId());
        model.setAreaIds(form.getAreaIds());
        model.setArea(form.getArea());
        model.setAddress(form.getAddress());
        supplierInfoService.updateById(model);
        return ResponseBean.success(model);
    }

    /**
     * 获取验证码
     */
    @ApiOperation(value = "获取验证码")
    @RequestMapping(value = "/code",method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean code(@SessionAttribute SupplierToken tokenUser) throws Exception {
        accountInfoService.createCheckCode(tokenUser.getUsername());
        return ResponseBean.success();
    }

    @ApiOperation(value = "设置密码")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/set-password",method = RequestMethod.POST)
    public ResponseBean setPassword(@RequestBody UpdatePaswordRequest request,@SessionAttribute SupplierToken tokenUser) throws Exception{
        if(StringUtils.isEmpty(request.getCode())){
            return ResponseBean.error("请输入短信验证码!");
        }
        AccountInfo model = accountInfoService.getById(tokenUser.getMemberId());
        if(!accountInfoService.checkCode(model.getMobile(),request.getCode())){
            return ResponseBean.error("验证码不正确!");
        }
        if(!request.getNewPassword().equals(request.getConfirmPassword())){
            return ResponseBean.error("新密码与确认密码不一致!");
        }
        model.setPassword(bCryptPasswordEncoder.encode(request.getNewPassword()));
        accountInfoService.updateById(model);
        return ResponseBean.success();
    }


    /**
     * 退出
     * @param request
     * @param tokenUser
     * @return
     */
    @ApiOperation(value = "退出")
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public ResponseBean logout(HttpServletRequest request,@SessionAttribute SupplierToken tokenUser){
        request.getSession().removeAttribute(ApiConstants.SESSION_PREFIX);
        return ResponseBean.success();
    }
}
